﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NotAClue.Web;
using System.Web.Security;

namespace DD_Extensions
{
    public partial class Login : System.Web.UI.Page
    {
        private const string INVALID_USERNAME_OR_PASSWORD = "Invalid username or password.";

        protected void Page_Load(object sender, EventArgs e)
        {
            System.Web.Security.FormsAuthentication.SignOut();

            //lblVersionDisplay.Text = Global.ApplicationVersion;

            //Title = TD.GetSnippet("Login.Title.snippet1");
            //if (!DD_Extensions.TD.IsLoginEnabled())
            //    Login1.Enabled = false;
            Title = "Login";
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            var UserName = Login1.FindControlRecursive("UserName") as WebControl;
            UserName.SetFocusHere();
        }

        private bool JavaScriptIsEnabled()
        {
            var hfJavaScriptEnabled = (HiddenField)Master.FindControl("JavaScriptEnabled");
            if (hfJavaScriptEnabled != null && hfJavaScriptEnabled.Value == "True")
                return true;
            else
                return false;
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (!JavaScriptIsEnabled())
            {
                Login1.Enabled = false;
                string s = "Your browser will need to run JavaScript for this system to work but JavaScript is not currently enabled.<br />Please check your browser settings.";
                Page.AddValidationError(s);
            }
        }

        protected void LoginUser_LoginError(object sender, EventArgs e)
        {
            Page.AddValidationError(INVALID_USERNAME_OR_PASSWORD);

            var Password = Login1.FindControlRecursive("Password") as WebControl;
            if (Password != null)
                Password.SetFocusHere();
        }
    }
}